package com.wu.serviceImpl.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wu.base.ApiResponse;
import com.wu.base.ResultUtils;
import com.wu.entity.sys.SPositionDo;
import com.wu.entity.sys.SUserPositionDo;
import com.wu.enums.SystemEnum;
import com.wu.enums.field.FlagEnum;
import com.wu.mapper.SPositionMapper;
import com.wu.mapper.SUserPositionMapper;
import com.wu.pojo.reqeust.BaseRequest;
import com.wu.pojo.reqeust.CommonIdRequest;
import com.wu.pojo.reqeust.CommonIdStatusRequest;
import com.wu.pojo.reqeust.sys.DeletePositionRequest;
import com.wu.pojo.reqeust.sys.ListPositionRequest;
import com.wu.pojo.reqeust.sys.SavePositionRequest;
import com.wu.pojo.reqeust.sys.UpdatePositionRequest;
import com.wu.pojo.response.sys.PositionListResponse;
import com.wu.service.sys.SPositionService;
import com.wu.util.DozerUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 职务表(SPosition)表服务实现类
 *
 * @since 2020-12-29 13:20:27
 */
@Service("sPositionService")
public class SPositionServiceImpl extends ServiceImpl<SPositionMapper, SPositionDo> implements SPositionService {
    @Resource
    private SPositionMapper sPositionMapper;

    @Resource
    private SUserPositionMapper sUserPositionMapper;

    @Override
    public ApiResponse listPosition(ListPositionRequest request) {
        PageHelper.startPage(request.getCurrentPage(), request.getPageSize());
        List<SPositionDo> sPositionListByName = sPositionMapper.getSPositionListByName(request.getPositionName());
        List<PositionListResponse> positionListResponses = new ArrayList<>();
        for (SPositionDo positionDo : sPositionListByName) {
            PositionListResponse positionListResponse = new PositionListResponse();
            positionListResponse.setId(positionDo.getId());
            positionListResponse.setName(positionDo.getName());
            positionListResponse.setDescription(positionDo.getDescription());
            positionListResponse.setNumber(countPositionUser(positionDo.getId()));
            positionListResponses.add(positionListResponse);
        }
        return ResultUtils.setOk(positionListResponses, new PageInfo<>(sPositionListByName));
    }

    @Override
    public ApiResponse optionList(BaseRequest request) {
        List<SPositionDo> sPositionListByName = sPositionMapper.getSPositionListByName(null);
        List<PositionListResponse> positionListResponses = DozerUtil.mapList(sPositionListByName, PositionListResponse.class);
        return ResultUtils.setOk(positionListResponses);
    }

    @Override
    public ApiResponse edit(UpdatePositionRequest request, Long userId) {
        int count = sPositionMapper.judgeIsExistPositionName(request.getPositionName(), request.getPositionId());
        if (count > 0) {
            return ResultUtils.setError(SystemEnum.POSITION_EXIST);
        }

        SPositionDo sPositionDo = sPositionMapper.selectById(request.getPositionId());
        if (null == sPositionDo || FlagEnum.DELETE.getCode().equals(sPositionDo.getFlag())) {
            return ResultUtils.setError(SystemEnum.DATA_NOT_EXIST);
        }
        sPositionDo.setModifier(userId);
        sPositionDo.setName(request.getPositionName());
        sPositionDo.setDescription(request.getDescription());
        sPositionDo.setModifyTime(new Date());
        int updateById = sPositionMapper.updateById(sPositionDo);
        if (updateById < 1) {
            return ResultUtils.setError(SystemEnum.FAILED);
        }
        return ResultUtils.setOk();
    }

    @Override
    public ApiResponse save(SavePositionRequest request, Long userId) {
        String positionName = request.getPositionName().trim();
        int count = sPositionMapper.judgeIsExistPositionName(positionName, null);
        if (count > 0) {
            return ResultUtils.setError(SystemEnum.POSITION_EXIST);
        }

        SPositionDo sPositionDo = new SPositionDo();
        sPositionDo.setName(positionName);
        sPositionDo.setCreater(userId);
        sPositionDo.setDescription(request.getDescription());
        int insert = sPositionMapper.insert(sPositionDo);
        if (insert < 1) {
            return ResultUtils.setError(SystemEnum.FAILED);
        }
        return ResultUtils.setOk();
    }

    @Override
    public ApiResponse deletePosition(DeletePositionRequest request, Long userId) {
        QueryWrapper<SPositionDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("flag", FlagEnum.NORMAL.getCode())
                .eq("id", request.getPositionId());
        SPositionDo sPositionDo = sPositionMapper.selectOne(queryWrapper);
        if (sPositionDo == null) {
            return ResultUtils.setError(SystemEnum.DATA_NOT_EXIST);
        }

        int count = sUserPositionMapper.countUsePositionByPositionId(request.getPositionId());
        if (count > 0) {
            return ResultUtils.setError(SystemEnum.POSITION_BE_USED);
        }
        sPositionDo.setModifier(userId);
        sPositionDo.setFlag(FlagEnum.DELETE.getCode());
        sPositionDo.setModifyTime(new Date());
        int i = sPositionMapper.updateById(sPositionDo);
        if (i < 1) {
            return ResultUtils.setError(SystemEnum.FAILED);
        }
        return ResultUtils.setOk();
    }

    @Override
    public ApiResponse onOrOff(CommonIdStatusRequest request) {
        return null;
    }

    @Override
    public ApiResponse getById(CommonIdRequest request) {
        QueryWrapper<SPositionDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("flag", FlagEnum.NORMAL.getCode())
                .eq("id", request.getId());
        SPositionDo sPositionDo = sPositionMapper.selectOne(queryWrapper);
        if (sPositionDo == null) {
            return ResultUtils.setError(SystemEnum.DATA_NOT_EXIST);
        }
        PositionListResponse map = DozerUtil.map(sPositionDo, PositionListResponse.class);
        map.setNumber(countPositionUser(map.getId()));
        return ResultUtils.setOk(map);
    }

    @Override
    public ApiResponse positionUserList(CommonIdRequest request) {
        return ResultUtils.setOk(sPositionMapper.getUsersByPositionId(request.getId()));
    }

    private int countPositionUser(Long positionId) {
        QueryWrapper<SUserPositionDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("flag", FlagEnum.NORMAL.getCode())
                .eq("position_id", positionId);
        return sUserPositionMapper.selectCount(queryWrapper);
    }
}